<template>
  <div class="list-container">
    <div class="search-wrap clearfix">
      <div class="search-wrap-conditions fl">
        <el-input
          v-model="listQuery.condition.keyword"
          style="width: 220px;"
          placeholder="关键词, ID、手机、用户名"
          clearable
          @keyup.enter.native="resetPage"
        />
      </div>
      <div class="search-wrap-btn fr">
        <el-button :disabled="loading" @click="resetSearch">重置</el-button>
        <el-button :disabled="loading" type="primary" @click="resetPage">搜索</el-button>
      </div>
    </div>
    <div class="main-wrapper">
      <custom-table :data="listData" :columns="columns">
        <template slot-scope="{ row }">
          <el-button type="text" @click="getDetail(row.userId)">查看详情</el-button>
        </template>
      </custom-table>
      <pagination :total="total" :custom-total="customTotal" @pagination="pagination" />
    </div>
  </div>
</template>

<script>
import CustomTable from '@/components/CustomTable/index';
import Pagination from '@/components/Pagination/index';
import { getUsersList } from '@/api/users';

export default {
  name: 'List',
  components: {
    CustomTable,
    Pagination
  },
  data () {
    return {
      loading: false,
      listQuery: {
        condition: {
          keyword: null
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      },
      customTotal: true,
      listData: [],
      total: 0,
      columns: [
        { label: '序号', value: 'index', align: 'center', width: '80px' },
        { label: 'ID', value: 'userId', align: 'center' },
        { label: '手机', value: 'mobileNumber', align: 'center' },
        { label: '用户名', value: 'name', align: 'center' },
        {
          label: '资质',
          value: 'teacherCertType',
          align: 'center',
          width: '90px'
        },
        {
          label: '平台认证',
          value: 'authenticatedTxt',
          align: 'center',
          width: '90px'
        },
        {
          label: '账号状态',
          value: 'statusTxt',
          align: 'center',
          width: '90px'
        },
        { label: '所在学校', value: 'school', align: 'center' },
        {
          label: '操作',
          value: 'todo',
          align: 'center',
          width: '90px'
        }
      ]
    };
  },
  created () {
    this.init();
  },
  methods: {
    resetPage () {
      this.listQuery.pager.page = 1;
      this.listQuery.pager.pageSize = 10;
      this.init();
    },
    init () {
      this.loading = true;
      getUsersList(this.listQuery)
        .then(res => {
          if (this.checkArr(res.data.records)) {
            res.data.records.map((item, index) => {
              item.statusTxt = item.status === 'ENABLE' ? '正常' : '冻结';
              item.authenticatedTxt = item.authenticated ? '认证' : '未认证';
            });
          }
          this.listData = res.data.records;
          this.total = res.data.total;
          this.loading = res.loading;
        })
        .catch(error => {
          this.loading = false;
        });
    },
    resetSearch () {
      this.listQuery = {
        condition: {
          keyword: null
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      };
      this.init();
    },
    pagination ({ page, limit }) {
      this.listQuery.pager.page = page;
      this.listQuery.pager.pageSize = limit;
      this.init();
    },
    getDetail (id) {
      this.$router.push({
        path: `/user/UserDetails`,
        query: { param: id }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
